//
//  WSPlayVideoViewController.m
//  RunningMan
//
//  Created by 王帅 on 16/5/25.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSPlayVideoViewController.h"
#import "WSMyTool.h"
#import <AVFoundation/AVFoundation.h>
// 正常屏幕播放视频的View
#import "CYVideoPlayerView.h"

// 全屏播放视频的View
#import "CYFullViewController.h"

#import "Masonry.h"

@interface WSPlayVideoViewController ()<CYVideoPlayerDelegate>
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) CYVideoPlayerView *playerView;
@property (nonatomic, strong) CYFullViewController *fullViewController;

@end

@implementation WSPlayVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.title = self.videoTitle;
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    // 创建正常屏幕播放视频View
    [self createrPlayerView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    
}
- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    [self.playerView.player pause];
    
}
#pragma mark - _创建播放视频的View
- (void)createrPlayerView {
    self.playerView = [[CYVideoPlayerView alloc] init];
    self.playerView.delegate = self;
    [self.view addSubview:self.playerView];
    self.playerView.frame = CGRectMake(0, 0, WIDTH, HEIGHT * 0.4);
    self.playerView.backgroundColor = [UIColor blackColor];
    [self.playerView replaceAVPlayerItem:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.Mp4Link]]];

}

#pragma mark - _创建全屏模式View
- (void)cyVideoToolBarView:(CYVideoToolBar *)cyVideoToolBar shouldFullScreen:(BOOL)isFull {
    if (isFull) {
        self.fullViewController = [[CYFullViewController alloc] init];
        [self.fullViewController.view addSubview:self.playerView];
        [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.removeExisting = YES;
        }];
        [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.equalTo(self.fullViewController.view);
        }];
        [self presentViewController:self.fullViewController animated:NO completion:nil];
    } else {
        [self.fullViewController dismissViewControllerAnimated:NO completion:^{
            [self.view addSubview:self.playerView];
            self.fullViewController = nil;
            [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.equalTo(self.view);
                make.height.mas_equalTo(HEIGHT * 0.4);
            }];
        }];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
